﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SalaryChannel
{
    public partial class DownloadPage : ChildWindow
    {
        private string _serverFile;
        private string _fileName;
        private Stream clientStream;
        public DownloadPage(string serverFile, string fileName)
        {
            InitializeComponent();

            Init(serverFile, fileName);
        }

        private void Init(string serverFile,string fileName)
        {
            _serverFile = serverFile;
            _fileName = fileName;
            progressBar.Visibility = System.Windows.Visibility.Visible;
            infoTB.Visibility = System.Windows.Visibility.Collapsed;

            SaveFileDialog save = new SaveFileDialog();
            save.DefaultFileName = fileName;
            if (save.ShowDialog().Value)
            {
                clientStream = save.OpenFile();
                WebClient client = new WebClient();
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(OnOpenReadCompleted);
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clientDownloadStream_DownloadProgressChanged);
                client.OpenReadAsync(new Uri(_serverFile));
                progressBar.Value = progressBar.Minimum;
            }
            else
            {
                this.DialogResult = false;

            }
        }

        private void clientDownloadStream_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
           // this.tbMsgString.Text = string.Format("完成百分比：{0} 当前收到的字节数：{1} 资料大小：{2} ",
           //e.ProgressPercentage.ToString() + "%",
           //e.BytesReceived.ToString(),
           //e.TotalBytesToReceive.ToString());
           progressBar.Value = e.ProgressPercentage;
            //try
            //{
               
            //    if (e.ProgressPercentage == 100)
            //    {
            //        ShowMsg("下载完毕！");
            //    }
            //}
            //catch (Exception ex)
            //{

            //    ShowMsg(ex.Message);
            //}
        }

        private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                ShowMsg(e.Error.ToString());
                return;
            }
            if (e.Cancelled != true)
            {
                Stream serverStream = e.Result;
                byte[] buffer = new byte[serverStream.Length];
                serverStream.Read(buffer, 0, buffer.Length);
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Close();
                serverStream.Close();
                ShowMsg("下载完毕！");
            }
        }

        private void ShowMsg(string msg)
        {
          //  progressBar.Visibility = System.Windows.Visibility.Collapsed;
            infoTB.Visibility = System.Windows.Visibility.Visible;
            infoTB.Text = msg + " 单击返回";
        }
        private void infoTB_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            this.DialogResult = true;
        }

    }
}
